package com.bootdo.wx.controller;

import com.bootdo.wx.domain.CmsMaterialDO;
import com.bootdo.wx.domain.GoodsGoodsDO;
import com.bootdo.wx.domain.LessonContentDO;
import com.bootdo.wx.service.CmsMaterialService;
import com.bootdo.wx.service.GoodsGoodsService;
import com.bootdo.wx.service.LessonContentService;
import com.bootdo.common.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 官方素材
 * 
 * @author sys
 * @email 1992lcg@163.com
 * @date 2019-04-03 11:12:34
 */
 
@Controller
@RequestMapping("/wx/cmsMaterial")
public class CmsMaterialController {
	@Autowired
	private CmsMaterialService cmsMaterialService;
	@Autowired
	private GoodsGoodsService goodsGoodsService;
	@Autowired
	private LessonContentService lessonContentService;
	
	@GetMapping()
	@RequiresPermissions("wx:cmsMaterial:cmsMaterial")
	String CmsMaterial(){
	    return "wx/cmsMaterial/cmsMaterial";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:cmsMaterial:cmsMaterial")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<CmsMaterialDO> cmsMaterialList = cmsMaterialService.list(query);
		if (cmsMaterialList.size()>0){
			for (CmsMaterialDO cmsMaterialDO:cmsMaterialList){
				String goodsId = cmsMaterialDO.getGoodsId();
				GoodsGoodsDO goodsGoodsDO = goodsGoodsService.get(goodsId);
				if (goodsGoodsDO!=null){
					cmsMaterialDO.setGoodsId(goodsGoodsDO.getName());
				}
				LessonContentDO lessonContentDO = lessonContentService.get(goodsId);
				if (lessonContentDO!=null){
					cmsMaterialDO.setGoodsId(lessonContentDO.getName());
				}

			}
		}
		int total = cmsMaterialService.count(query);
		PageUtils pageUtils = new PageUtils(cmsMaterialList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:cmsMaterial:add")
	String add(){
	    return "wx/cmsMaterial/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:cmsMaterial:edit")
	String edit(@PathVariable("id") String id,Model model){
		CmsMaterialDO cmsMaterial = cmsMaterialService.get(id);
		String img = cmsMaterial.getImages();
		if (StringUtils.isNotBlank(img)){
			model.addAttribute("cmsMaterialImg", img.split(","));
		}else {
			model.addAttribute("cmsMaterialImg", "".split(","));
		}
		model.addAttribute("cmsMaterial", cmsMaterial);
		
		List<Map<String,Object>> list = goodsGoodsService.queryAllForIdAndName(new HashMap<String,Object>());
		model.addAttribute("goodsList", list);
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("sort", "location");
		map.put("order", "asc");
		map.put("delFlag", "0");
		List<LessonContentDO> contentList = lessonContentService.list(map);
		model.addAttribute("contentList", contentList);
	    return "wx/cmsMaterial/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:cmsMaterial:add")
	public R save( CmsMaterialDO cmsMaterial){
		cmsMaterial.setId(UuidUtil.get32UUID());
		cmsMaterial.setDelFlag(0);
		cmsMaterial.setCreateAt(new Date());
		/*String img = cmsMaterial.getImages();
		img = img.replaceFirst(",","");
		cmsMaterial.setImages(img);*/
		
		String goodsId = cmsMaterial.getGoodsId();
		String[] split = goodsId.split(",");
		if(cmsMaterial.getMode()==2){
			cmsMaterial.setGoodsId("");
		}else if(cmsMaterial.getMode()==0){
			for (String string : split) {
				GoodsGoodsDO goodsGoodsDO = goodsGoodsService.get(string);
				if(goodsGoodsDO!=null){
					cmsMaterial.setGoodsId(string);
				}
			}
		}else if(cmsMaterial.getMode()==1){
			for (String string : split) {
				LessonContentDO lessonContentDO = lessonContentService.get(string);
				if(lessonContentDO!=null){
					cmsMaterial.setGoodsId(string);
				}
			}
		}
		if(cmsMaterialService.save(cmsMaterial)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:cmsMaterial:edit")
	public R update( CmsMaterialDO cmsMaterial){
		cmsMaterial.setCreateAt(new Date());
		/*String img = cmsMaterial.getImages();
		img = img.replaceFirst(",","");
		cmsMaterial.setImages(img);*/
		
		String goodsId = cmsMaterial.getGoodsId();
		String[] split = goodsId.split(",");
		if(cmsMaterial.getMode()==2){
			cmsMaterial.setGoodsId("");
		}else if(cmsMaterial.getMode()==0){
			for (String string : split) {
				GoodsGoodsDO goodsGoodsDO = goodsGoodsService.get(string);
				if(goodsGoodsDO!=null){
					cmsMaterial.setGoodsId(string);
				}
			}
		}else if(cmsMaterial.getMode()==1){
			for (String string : split) {
				LessonContentDO lessonContentDO = lessonContentService.get(string);
				if(lessonContentDO!=null){
					cmsMaterial.setGoodsId(string);
				}
			}
		}
		cmsMaterialService.update(cmsMaterial);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:cmsMaterial:remove")
	public R remove( String id){
		if(cmsMaterialService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:cmsMaterial:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		cmsMaterialService.batchRemove(ids);
		return R.ok();
	}
	
}
